package org.flowable.app.cusTaskExe;

import cmbchina.util.AjaxJson;
import cmbchina.util.JacksonUitl;
import cmbchina.util.db.FlowDb;
import cmbchina.util.db.FlowFormDb;
import cmbchina.util.http.DbServiceRequestUtil;
import org.flowable.app.model.runtime.CompleteFormRepresentation;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sevenlm on 2017/12/22.
 */
public class CusCheckItilTask implements CusTask {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CusCheckItilTask.class);
    @Override
    public AjaxJson exe( CompleteFormRepresentation completeTaskFormRepresentation, TaskService taskService, String taskId, String resultParmas, String processInstId,String taskDefinitionKey) {

        logger.info("==============checkItil start=============");
        //将流程状态置为运行中
        FlowDb.updateFlowState(2, processInstId);
        AjaxJson ajaxJson =new AjaxJson();
        String script = "";
        if(completeTaskFormRepresentation.getValues().get("itilNum")==null){
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("该任务步骤没有itil单号");
            logger.info("该任务步骤没有itil单号");
            //将流程状态置为运行失败
            FlowDb.updateFlowState(4, processInstId);
            //将任务节点改为失败
            FlowFormDb.updateFlowFormState(4,processInstId,taskDefinitionKey,"9527",ajaxJson.getMsg(),script);
            taskService.setVariableLocal(taskId, "result", ajaxJson.getMsg());
            return  ajaxJson;
        }
        String itilNum = completeTaskFormRepresentation.getValues().get("itilNum").toString();

        //跟新缓存
        try {
            Map<String,Object> mapScriptParams = new HashMap<String, Object>();
            mapScriptParams.put("itilNum",itilNum);
            String jsonParams = JacksonUitl.getObjectMapper().writeValueAsString(mapScriptParams);
            ajaxJson = new DbServiceRequestUtil().checkItilNum(jsonParams);
            taskService.setVariable(taskId,"returnCode",ajaxJson.getCode());
            taskService.setVariableLocal(taskId, "result", ajaxJson.getMsg());
            logger.info("配置了输出："+resultParmas);
//            if(!ajaxJson.isSuccess()){
//                //将任务节点改为失败
//                FlowFormDb.updateFlowFormState(4,processInstId,taskDefinitionKey,ajaxJson.getCode(),ajaxJson.getMsg());
//            }
//            if(resultParmas!=null&&!"".equals(resultParmas)){
//                taskService.setVariable(taskId,resultParmas,ajaxJson.getCode());
//
//            }
//            else{
//                ajaxJson.setSuccess(true);
//            }



        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setSuccess(false) ;
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setCode("400");
            taskService.setVariable(taskId, "returnCode", "400");
            taskService.setVariableLocal(taskId, "result", e.getMessage());
//            formService.saveFormInstanceByFormModelId(completeTaskFormRepresentation.getValues(), completeTaskFormRepresentation.getFormId(), taskId, task.getProcessInstanceId());
        }
        if(ajaxJson.isSuccess()){
            taskService.completeTaskWithForm(taskId, completeTaskFormRepresentation.getFormId(),
                    completeTaskFormRepresentation.getOutcome(), completeTaskFormRepresentation.getValues());
            System.out.println("task count"+taskService.createTaskQuery().processInstanceId(processInstId).count());
            //将任务节点改为成功
            FlowFormDb.updateFlowFormState(5,processInstId,taskDefinitionKey,ajaxJson.getCode(),ajaxJson.getMsg(),script);
            if(FlowFormDb.isEnd(processInstId)){
                logger.info("=======判断是否执行完成====");

                //将流程状态置为运行成功
                FlowDb.updateFlowState(5, processInstId);
            }
        }else{
            //将任务节点改为成功
            FlowFormDb.updateFlowFormState(4,processInstId,taskDefinitionKey,ajaxJson.getCode(),ajaxJson.getMsg(),script);
            //将流程状态置为运行失败
            FlowDb.updateFlowState(4, processInstId);
        }
        logger.info("==============checkItil end=============");

        return  ajaxJson;
    }
}
